package com.ruoyi.common.utils;

import java.io.IOException;

import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;
import org.springframework.core.io.ClassPathResource;

public class FastDFSUtils {
	 private static TrackerClient trackerClient = null;
	 private static StorageServer storageServer = null;
	 static {
		  /**
		   *  加载配置文件
		   */
		  try {
		   ClassPathResource resource = new ClassPathResource("fdfs_client.conf");
		   ClientGlobal.init(resource.getClassLoader().getResource("fdfs_client.conf").getPath());
		   trackerClient = new TrackerClient();
		  } catch (IOException | MyException e) {
		   e.printStackTrace();
		  }
		 }
	 

	 /**
	  * 上传文件
	  * fileBuff:二进制文件
	  * filePath:文件路径，上传本地文件，测试用
	  * fileName:文件名称，用来获取文件后缀
	  * fileBuff和filePath二选一
	  * uploadFile(filebuff,null,filename)|uploadFile(null,filepath,filename)
	 * @throws IOException 
	  * */
	 public static String[] uploadFile(byte[] fileBuff, String filePath, String fileName) throws IOException {
		 TrackerServer trackerServer = trackerClient.getConnection();
		 StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		  try {
		   if (fileBuff == null && filePath == null) {
		    return new String[0];
		   }
		   // 获取文件扩展名称
		   String fileExtName = "";
		   if (fileName != null && !"".equals(fileName) && fileName.contains(".")) {
		    fileExtName = fileName.substring(fileName.lastIndexOf(".") + 1);
		   } else {
		    return new String[0];
		   }
	
		   // 上传文件
		   String[] FileIds = null;
		   if (fileBuff != null && filePath == null) {
		    if (fileBuff.length == 0) {
		     return new String[0];
		    }
		    FileIds = storageClient.upload_file(fileBuff, fileExtName, null);
		    } else {
		    FileIds = storageClient.upload_file(filePath, fileExtName, null);
		   }
		   return FileIds == null ? new String[0] : FileIds;
		  } catch (Exception e) {
		   e.printStackTrace();
		  } finally {
		   try {
		    if (trackerServer != null) {
		     trackerServer.close();
		     trackerServer = null;
		    }
		    if (storageServer != null) {
		     storageServer.close();
		     storageServer = null;
		    }
		   } catch (IOException e) {
		    e.printStackTrace();
		   }
		  }
		  return new String[0];
		 }
	 
	 public static Integer deleteFile(String filePath) throws IOException, MyException {
		 int result = -1;
		 TrackerServer trackerServer = trackerClient.getConnection();
		 StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		 if(!filePath.equals("")) {
			 int index = filePath.indexOf("/");
			 String group = filePath.substring(0, index);
			 String storagePath = filePath.substring(index+1);
			 result = storageClient.delete_file(group, storagePath);
		 }
		 return result;
	 }
	 
	 public static byte[] download(String filePath) throws IOException, MyException {
		 byte[] result;
		 TrackerServer trackerServer = trackerClient.getConnection();
		 StorageClient storageClient = new StorageClient(trackerServer, storageServer);
		 if(!filePath.equals("")) {
			 int index = filePath.indexOf("/");
			 String group = filePath.substring(0, index);
			 String storagePath = filePath.substring(index+1);
			 result = storageClient.download_file(group, storagePath);
			 return result;
		 }
		 return null;
	 }

}
